//
//  ViewController.m
//  PopView
//
//  Created by lanou on 16/1/8.
//  Copyright © 2016年 FJY. All rights reserved.
//

#import "ViewController.h"
#import "FJYPopView.h"

@interface ViewController ()

@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) UIView *curveView;
@property (nonatomic, assign) CGFloat curveX;
@property (nonatomic, assign) CGFloat curveY;
@property (nonatomic, assign) CGFloat mHeight;
@property (nonatomic, assign) BOOL isAnimating;
@property (nonatomic, strong) CADisplayLink *displayLink;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [FJYPopView popViewShow:self.view title:@"Prompt" detail:@"This is a prompt! you konw ,I konw he don't konw." okButton:@"ok" cancelButton:@"cancel" okButtonCall:^(ButtonClick buttonIndex) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    } cancel:^(ButtonClick buttonIndex) {
        [[FJYPopView sharedFJYPopView] popViewDisappear];
    }];
    
//    [self configShapeLayer];
//    [self configCurveView];
//    [self configAction];
    
//    NSDate
}

- (void)configShapeLayer {
    _shapeLayer = [CAShapeLayer layer];
    _shapeLayer.fillColor = [UIColor purpleColor].CGColor;
    [self.view.layer addSublayer:_shapeLayer];
}

- (void)configCurveView {
    _curveX = 375/2;
    _curveY = 667/2;
    _curveView = [[UIView alloc] initWithFrame:CGRectMake(_curveX, _curveY, 3, 3)];
    _curveView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_curveView];
}

- (void)configAction {
    _mHeight = 100;
    _isAnimating = NO;
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanAction:)];
    self.view.userInteractionEnabled = YES;
    [self.view addGestureRecognizer:pan];
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(calculationPath)];
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    _displayLink.paused = YES;
}

- (void)handlePanAction:(UIPanGestureRecognizer *)pan {
    if (!_isAnimating) {
        if (pan.state == UIGestureRecognizerStateChanged) {
            CGPoint point = [pan translationInView:self.view];
            _mHeight = point.y * 0.7 + 667/2;
            _curveX = point.x + 375/2;
            _curveY = _mHeight > 667/2 ? _mHeight : 667/2;
            _curveView.frame = CGRectMake(_curveX, _curveY, _curveView.frame.size.width, _curveView.frame.size.height);
            [self updateShapeLayerPath];
        } else if (pan.state == UIGestureRecognizerStateCancelled||    pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateFailed) {
            _isAnimating = YES;
            _displayLink.paused = NO;
            [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
                _curveView.frame = CGRectMake(375/2, 667/2, 3, 3);
            } completion:^(BOOL finished) {
                if (finished) {
                    _displayLink.paused = YES;
                    _isAnimating = NO;
                }
            }];
        }
    }
}

- (void)updateShapeLayerPath {
    UIBezierPath *tPath = [UIBezierPath bezierPath];
    [tPath moveToPoint:CGPointMake(0, 0)];
    [tPath addLineToPoint:CGPointMake(375, 0)];
    [tPath addLineToPoint:CGPointMake(375, 667/2)];
    [tPath addQuadCurveToPoint:CGPointMake(0, 667/2) controlPoint:CGPointMake(_curveX, _curveY)];
    [tPath closePath];
    _shapeLayer.path = tPath.CGPath;
    
}

- (void)calculationPath {
    CALayer *layer = _curveView.layer.presentationLayer;
    _curveX = layer.position.x;
    _curveY = layer.position.y;
    [self updateShapeLayerPath];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
